package com.bosssoft.springcloud.controller;

import com.bosssoft.springcloud.entity.dto.UserDTO;
import com.bosssoft.springcloud.service.UserService;
import com.bosssoft.springcloud.entity.vo.CommenResult;
import com.bosssoft.springcloud.entity.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;

    /***
     * 查询所有用户信息
     * @return
     */
    @GetMapping("/rbac/user/getAll")
    public CommenResult getAllUsers(){
        List<UserDTO> allUsers = userService.findAllUsers();
        if (allUsers!=null){
            return new CommenResult(200,"查询成功",allUsers);
        }
        return new CommenResult(404,"查询失败",null);
    }

    /***
     * 查询单个用户信息
     * @param username 用户名
     * @return
     */
    @GetMapping("/rbac/user/getOne")
    public CommenResult getOneUser(@RequestBody String username){
        UserDTO user = userService.findUserByUsername(username);
        if (user!=null){
            return  new CommenResult(200,"查询成功",user);
        }
        return new CommenResult(404,"未找到该用户",null);
    }

    /***
     * 移除用户
     * @param userVO
     * @return
     */
    @PostMapping("/rbac/user/removeUser")
    public CommenResult removeUser(@RequestBody UserVO userVO){
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userVO,userDTO);
        int res = userService.removeUserById(userDTO.getId());
        if (res > 0){
            return  new CommenResult(200,"移除成功",res);
        }
        return new CommenResult(404,"移除失败",res);
    }

}
